package com.lkd.http.controller;

import com.lkd.common.VMSystem;
import com.lkd.entity.TaskDetailsEntity;
import com.lkd.entity.TaskEntity;
import com.lkd.entity.TaskStatusTypeEntity;
import com.lkd.entity.TaskTypeEntity;
import com.lkd.exception.LogicException;
import com.lkd.http.vo.*;
import com.lkd.service.*;
import com.lkd.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {
    @Autowired
    private TaskService taskService;

    @Autowired
    private TaskDetailsService taskDetailsService;

    @Autowired
    private TaskTypeService taskTypeService;


    /**
     * 搜索工单
     *
     * @param pageIndex
     * @param pageSize
     * @param innerCode 设备编号
     * @param userId    工单所属人Id
     * @param taskCode  工单编号
     * @param status    工单状态
     * @param isRepair  是否是维修工单
     * @return
     */
    @GetMapping("/search")
    public Pager<TaskEntity> search(
            @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Long pageIndex,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Long pageSize,
            @RequestParam(value = "innerCode", required = false, defaultValue = "") String innerCode,
            @RequestParam(value = "userId", required = false, defaultValue = "") Integer userId,
            @RequestParam(value = "taskCode", required = false, defaultValue = "") String taskCode,
            @RequestParam(value = "status", required = false, defaultValue = "") Integer status,
            @RequestParam(value = "isRepair", required = false, defaultValue = "") Boolean isRepair,
            @RequestParam(value = "start", required = false, defaultValue = "") String start,
            @RequestParam(value = "end", required = false, defaultValue = "") String end) {
        return taskService.search(pageIndex, pageSize, innerCode, userId, taskCode, status, isRepair, start, end);
    }


    /**
     * 根据taskId查询
     *
     * @param taskId
     * @return 实体
     */
    @GetMapping("/taskInfo/{taskId}")
    public TaskEntity findById(@PathVariable Long taskId) {
        return taskService.getById(taskId);
    }


    @GetMapping("/allTaskStatus")
    public List<TaskStatusTypeEntity> getAllStatus() {
        return taskService.getAllStatus();
    }

    /**
     * 获取工单类型
     *
     * @return
     */
    @GetMapping("/typeList")
    public List<TaskTypeEntity> getProductionTypeList() {
        return taskTypeService.list();
    }

    /**
     * 获取工单详情
     *
     * @param taskId
     * @return
     */
    @GetMapping("/details/{taskId}")
    public List<TaskDetailsEntity> getDetail(@PathVariable long taskId) {
        return taskDetailsService.getByTaskId(taskId);
    }

    /**
     * 创建工单
     *
     * @param task
     * @return
     */
    @PostMapping("/create")
    public boolean createTask(@RequestBody TaskViewModel task) {
        //调用创建工单业务需要先将指派人的id写进task中
        task.setAssignorId(getUserId());
        //调用创建工单业务
        return taskService.createTask(task);
    }

    /**
     * 接受工单
     *
     * @param taskId
     * @return
     */
    @GetMapping("/accept/{taskId}")
    public boolean acceptTask(@PathVariable String taskId) {
        TaskEntity taskEntity = taskService.getById(Long.valueOf(taskId));
        if (taskEntity.getUserId().intValue() != this.getUserId().intValue()) {
            throw new LogicException("操作非法");
        }
        //调用业务接受工单
        return taskService.acceptTask(Long.valueOf(taskId));
    }

    /**
     * 取消工单
     *
     * @param taskId
     * @param cancelTask
     * @return
     */
    @PostMapping("/cancel/{taskId}")
    public boolean cancelTask(@PathVariable String taskId, @RequestBody CancelTaskViewModel cancelTask) {
        Long id = Long.valueOf(taskId);
        TaskEntity taskEntity = taskService.getById(id);
        if (!(taskEntity.getUserId().intValue() == getUserId().intValue()
                || taskEntity.getAssignorId().intValue() == getUserId().intValue())) {
            throw new LogicException("非法操作");
        }
        //取消工单
        return taskService.cancelTask(id, cancelTask);
    }

    @GetMapping("/complete/{taskId}")
    public boolean completeTask(@PathVariable String taskId) {
        Long id = Long.valueOf(taskId);
        TaskEntity taskEntity = taskService.getById(id);
        if (taskEntity.getUserId().intValue() != getUserId().intValue()) {
            throw new LogicException("非法操作");
        }
        //完成工单
        return taskService.completeTask(id);
    }
}